package cn._2dland.uploader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.params.HttpClientParams;

public abstract class UploadClient {
	protected String username;
	protected String password;

	protected boolean bLogin;

	protected HttpClient client;

	public UploadClient(String user, String pass) {
		username = user;
		password = pass;

		// 初始化HttpClient，并设置若干初始参数
		client = new HttpClient();
		client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
		client.getParams().setBooleanParameter(HttpClientParams.SINGLE_COOKIE_HEADER, true);
		client.getParams().setParameter(HttpClientParams.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
		client.getParams().setContentCharset("utf-8");
	}

	public UploadClient(UserPassPair pair) {
		this(pair.getUsername(), pair.getPassword());
	}

	/**
	 * 判断是否登录
	 * @return
	 */
	public boolean isLogin() {
		return bLogin;
	}

	/**
	 * 登录帐户
	 * @return 是否登录成功
	 */
	abstract public boolean login();

	/**
	 * 执行上传任务
	 * @param task 上传任务
	 * @return
	 */
	abstract public boolean upload(UploadTask task);
}
